// SlagCar/DailyCheckNewAdmin/DailyCheckNewAdmin.js
let listBehavior = require('../../behavior/list')
Page({
  behaviors: [ listBehavior, ],

  /**
   * 页面的初始数据
   */
  data: {
    tabList: ['正常','异常','未检查'], //顶部标签
    tabActive: 0, //标签选中索引

    yearOptions: [],
    monthOptions: [],
    dayOptions: [],
    year: '',
    month: '',
    day: '',

    pageTitle: ''
  },
  //顶部菜单栏切换
  onTabTap(e){
    let { i, } = e.currentTarget.dataset,
      obj = {tabActive: i};
    this.setData(obj);
    this.getDataList();
  },

  //获取数据列表
  getDataList(){
    let {tabActive,year,month,day,type} = this.data,
      post = {query_item: tabActive + 1 },
      total = 0,
      obj = {};
    let url = `ZTAuth/TruckCheck2/${type === 'before' ? 'BeforeDriving':type === 'going' ? 'OnDriving' : 'AfterDriving'}/${year}-${month}-${day}`;
    this.setData({list: []})
    wx.showLoading({mask: true});
    wx.$get(5,url,post).then(res => {
      if (res.errcode != 0) return;
      wx.hideLoading();
      if (res.data.row){
        total = res.data.total;
        let rows = res.data.row;
        obj = {total,list: rows};
      }
      this.setData(obj);
    })
  },

  //获取日期参数
  getDateOptions(date){
    let today = date ? new Date(date) : new Date(),
      year = today.getFullYear(),
      month = today.getMonth() + 1,
      day = today.getDate(),
      yearOptions = [],
      monthOptions = [];
    today.setDate(1); //设为日期一号
    today.setMonth(month);  //设为下月
    today.setDate(0);   //设为本月最后一日

    let lastDay = today.getDate();//本月最后一天
    for (let y = 2023;y <= year; y++){
      yearOptions.push( { text: y, value: y },)
    }
    for (let m = 1;m <=12;m++){
      monthOptions.push({text: m + '月', value: m })
    }
    this.setData({year,month,day,yearOptions,monthOptions});
    this.setDayOptions();
  },
  //日期选择变化
  onSwitchChange(e){
    let {detail,currentTarget} = e,
      {field} = currentTarget.dataset;
    this.setData({[field]: detail,list: []});
    if (field !== 'day') this.setDayOptions();
    this.getDataList();
  },
  //获取每月天数选项
  setDayOptions(){
    let {year,month,day} = this.data,
      date = new Date(`${year}/${month}/01`),
      dayOptions = [];
    date.setMonth(month)
    date.setDate(0);
    let lastDay = date.getDate();
    for (let d = 1;d <= lastDay;d++){
      dayOptions.push({text: d + '日', value: d })
    }
    let obj = {dayOptions}
    if (day && day > lastDay) obj.day = lastDay
    this.setData(obj)
  },

  //跳转查看检查详情
  navTo(e){
    let {index} = e.currentTarget.dataset,
      {list,type} = this.data,
      data = list[index];
    if (data.report_result !== '正常' && data.report_result !== '异常') return;
    let url = `/SlagCar/DailyCheckNewDetl/DailyCheckNewDetl?rid=${data.report_id}&check_type=${type}`;
    wx.navigateTo({url})
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {} = this
    let {type,date} = options,
      pageTitle = '',tabList = ['正常','异常','未检查'];
    if (type === 'before') {
      pageTitle = '出车前检查';
      tabList =  ['正常','异常','未检查','未出车'] //顶部标签
    }
    if (type === 'going') pageTitle = '行车中检查'
    if (type === 'after') pageTitle = '收车后检查'

    this.setData({type,pageTitle,tabList})
    this.getDateOptions(date);
    this.getDataList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
